home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / v12n12.zip / EVAL.ZIP / EVAL.C next >
C/C++ Source or Header  |  1993-03-26  |  4KB  |  156 lines

  1. //****************************************************************
  2. //           EXPRESSION EVALUATOR                  
  3. //****************************************************************
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. #define false 0
  8. #define true 1
  9.  
  10. int NextChar;
  11. int contin = true;
  12.  
  13. float Factor(void);
  14. float Expression(void);
  15. float Term(void);
  16. //****************************************************************
  17. // GetNextChar - Reads characters until a non-space char is found
  18. //****************************************************************
  19. void GetNextChar(void) {
  20.     while ((NextChar = getchar()) == ' ');
  21. }
  22.  
  23. //****************************************************************
  24. // Expression - Recursive routine that evaluates an expression
  25. //     EXPRESSION = <EXPRESSION> + <TERM> | <EXPRESSION> - <TERM>
  26. //****************************************************************
  27. float Expression(void) {
  28.     float value;
  29.     value = Term();
  30.  
  31.     for (;;)    {
  32.        switch (NextChar) {
  33.             case ' ':
  34.                 GetNextChar(); 
  35.                 break;
  36.             case '+':
  37.                 GetNextChar(); 
  38.                 value = value + Term(); 
  39.                 continue;
  40.             case '-':
  41.                 GetNextChar(); 
  42.                 value = value - Term(); 
  43.                 continue;
  44.             default:
  45.                 return value;
  46.         }
  47.     }
  48. }
  49.  
  50. //****************************************************************
  51. // Term - Handles multiplication and division
  52. //  <TERM> = <TERM> * <FACTOR> | <TERM> DIV <FACTOR> | <FACTOR>
  53. //****************************************************************
  54. float Term(void) {
  55.     float value, divisor;
  56.  
  57.     value = Factor();
  58.     for(;;) {
  59.         switch(NextChar) {
  60.             case ' ': 
  61.                 GetNextChar(); 
  62.                 break;
  63.             case '*':
  64.                 GetNextChar(); 
  65.                 value = value * Factor(); 
  66.                 continue;
  67.             case '^':
  68.                 GetNextChar(); 
  69.                 value = pow(value, Factor()); 
  70.                 continue;
  71.             case '/':
  72.                 GetNextChar();
  73.                 if ((divisor = Factor()) != 0) {
  74.                     value = value / divisor; 
  75.                     continue;
  76.                 } else {
  77.                     printf ("  DIVISION BY ZERO\n");
  78.                     exit(0);
  79.                 }
  80.             default:
  81.                 return value;
  82.         }
  83.     }
  84. }
  85.  
  86. //****************************************************************
  87. // Factor - Handles numbers minus signs and parens
  88. //  <FACTOR> = <EXPRESSION> | <VARIABLE> | <CONSTANT>
  89. //****************************************************************
  90. float Factor(void) {
  91.     float value = 0;
  92.     int count = 0;
  93.     int i;
  94.     int d_point = false;
  95.  
  96.     if ((NextChar <= '9') && (NextChar >= '0')) {
  97.         while ((NextChar <= '9') && (NextChar >= '0')) {
  98.             value = value * 10 + NextChar - '0';
  99.             NextChar = getchar();
  100.             if (d_point)
  101.                 count++;
  102.             if (NextChar == '.') {
  103.                 NextChar = getchar();
  104.                 d_point = true;
  105.             }
  106.         }
  107.         for (i = 0; i < count; i++)
  108.             value = value / 10;
  109.         return value;
  110.     } else {
  111.         switch(NextChar) {
  112.             case '-':
  113.                 GetNextChar(); 
  114.                 return -1*Factor();
  115.             case '(':
  116.                 GetNextChar(); 
  117.                 value = Expression(); 
  118.                 if (NextChar != ')') { 
  119.                     printf ("  MISMATCHED PARENTHES\n");
  120.                     exit(0);
  121.                 } else
  122.                     NextChar = getchar(); 
  123.             return value;
  124.             case '.': 
  125.                 d_point = true;
  126.            default:
  127.                 contin = false;
  128.         }
  129.     }    
  130.     return 0;
  131. }
  132.  
  133. //****************************************************************
  134. // Main - Program entry point
  135. //****************************************************************
  136. void main () {
  137.     float result;
  138.  
  139.     printf ("\nExpression must not contain any other symbols but:\n");
  140.     printf("\n'+' - addition\n'-' - subtraction\n'*' - multiplication");
  141.     printf("\n'/' - division\n'(' - left parenthesis\n')' - right parenthesis");
  142.     printf("\n'.' - decimal point (must be preceeded by digit)");
  143.     printf ("\n'^' - x^y means x to the power of y\n\n");
  144.     printf ("\n\nENTER EXPRESSION:   ");
  145.  
  146.     GetNextChar ();
  147.     result = Expression();
  148.  
  149.      if ((NextChar == '\n') && (contin))
  150.        printf ("THE RESULT IS:   %1.3f\n", result);
  151.     else {
  152.        printf ("  SYNTAX ERROR\n");
  153.        exit(0);
  154.     }
  155. }
  156.